package com.example.springsecuritydemo.controller;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class MyController {


    @RequestMapping(value = "/login")
    public String login() {
        System.out.println("登陆成功");
        return "redirect:main.html";
    }

    /**
     * 登陆成功之后，进入主页
     * @return
     */
    @Secured(value = {"ROLE_test"})
    @RequestMapping(value = "/toMain")
    public String tiMain(){
        return "redirect:main.html";
    }

    /**
     * 登录失败，进入错误页面
     * @return
     */
    @RequestMapping(value = "/toError")
    public String toError(){
        return "redirect:error.html";
    }

    /**
     * 测试接口，测试放行post请求的接口，此接口为get请求，不放行
     * @return
     */
    @PreAuthorize("hasAnyRole('ROLE_test')")
    @GetMapping(value = "/test")
    @ResponseBody
    public String test(){
        return "test in";
    }
}
